var UserData = require('UserData')
cc.Class({
    extends: cc.Component,

    properties: {

    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.OriginalPosObj = new Object()
        UserData.runtimeState.GyroscopeFunctionList.push([this.updateResPos,this])
        this.time = 0
    },
    updateResPos(self,differ){
        if(!self.node.getComponent('Restaurant').resSetup){
            return
        }else{
            if(!self.resContentOriginalPos){
                self.resContentOriginalPos = cc.v2(0,0)
            }else if(self.resContentOriginalPos.mag()>20){
                self.resContentOriginalPos = cc.v2(0,0)
            }
            let xLimit = 13
            let yLimit = 0
            let moveRatio =20
            self.resContent = self.node.getChildByName('mask').getChildByName('背景-AnimationCancel')
            if(self.resContent){
                if(!self.desX){
                    self.desX = self.resContent.x
                }
                if(!self.desY){
                    self.desY = self.resContent.y
                }
                if(differ.y>1){
                    differ.y = 1
                }else if(differ.y<-1){
                    differ.y = -1
                }
                self.desX += differ.y * moveRatio
                self.desY += differ.x * moveRatio
                
                
                if(self.desX > self.resContentOriginalPos.x + xLimit){
                    self.desX = self.resContentOriginalPos.x + xLimit
                }
                if(self.desX < self.resContentOriginalPos.x - xLimit){
                    self.desX = self.resContentOriginalPos.x - xLimit
                }

                
                if(self.desY > self.resContentOriginalPos.y + yLimit){
                    self.desY = self.resContentOriginalPos.y + yLimit
                }
                if(self.desY < self.resContentOriginalPos.y - yLimit){
                    self.desY = self.resContentOriginalPos.y - yLimit
                }
                if(self.resContentOriginalPos.sub(cc.v2(self.desX,self.resContentOriginalPos.y)).mag()>30){//超出范围
                    self.resContent.pos = self.resContentOriginalPos
                }else{//开始移动
                    self.resContent.runAction(cc.moveTo(0.1,cc.v2(self.desX,self.resContentOriginalPos.y)))
                    for(let i=0;i<self.resContent.children.length;i++){
                        let name = self.resContent.children[i].name
                        let uuid = self.resContent.children[i].uuid
                        if(!self.OriginalPosObj[uuid]){
                            self.OriginalPosObj[uuid] = cc.v2(self.resContent.children[i].x,self.resContent.children[i].y)
                        }
                        self.resContent.children[i].stopAllActions()
                        if(name!='Customer' && name.search("-f0") != -1){
                            self.resContent.children[i].runAction(cc.moveTo(0.1,cc.v2(-self.desX + self.OriginalPosObj[uuid].x ,-self.desY + self.OriginalPosObj[uuid].y)))                            
                        }
                        if(name!='Customer' && name.search("-f1") != -1){
                            self.resContent.children[i].runAction(cc.moveTo(0.1,cc.v2(- 3 *self.desX + self.OriginalPosObj[uuid].x ,- 2 * self.desY + self.OriginalPosObj[uuid].y)))
                        }
                    }
                    
                }
                //if(differ.y * moveRatio  +differ.x * moveRatio>5){
                    


                //}
                
            }
        }
    },
    moveCalc(self,obj,depth){
        if(obj.children.length ==0){
            return
        }else{

        }
    },
    /*update (dt) {
        this.time+=dt
        if(this.time > 1){
            this.updateResPos(this,cc.v2(Math.random()-0.5,Math.random()-0.5))
            this.time = 0
        }
        
    },*/
});
